﻿Imports System
Imports System.Text
Imports System.Web
Imports System.Web.UI
Imports System.Threading
Imports System.IO
Imports System.ComponentModel
Imports System.Web.UI.HtmlControls

<ToolboxData("<{0}:SolidQCaptcha runat=server></{0}:SolidQCaptcha>")> _
Public Class SolidQCaptcha
    Inherits Control

#Region "Global Variables"

    Private Enum languages
        english
        espanish
        german
        french
        portuguese
        italian
    End Enum
#End Region

#Region "General Incoming Parameters"

    Private _Language As String = "english"
    <Category("Behaviour"), _
    PersistenceMode(PersistenceMode.Attribute), _
    DesignerSerializationVisibility(DesignerSerializationVisibility.Visible), _
    Description("Type the language of the captcha: english or espanish.")> _
    Public Property Language() As String
        Get
            Return _Language
        End Get
        Set(ByVal value As String)
            _Language = value
        End Set
    End Property

    Private _InputID As String = String.Empty
    <Category("Behaviour"), _
    PersistenceMode(PersistenceMode.Attribute), _
    DesignerSerializationVisibility(DesignerSerializationVisibility.Visible), _
    Description("Type the ID of the Input where saves the variables to compare.")> _
    Public Property InputID() As String
        Get
            Return _InputID
        End Get
        Set(ByVal value As String)
            _InputID = value
        End Set
    End Property

#End Region


    Protected Overrides Sub CreateChildControls()
        Try
            Dim r As New Random

            'Adding styles
            Dim myHtmlLink As HtmlLink = New HtmlLink
            myHtmlLink.Attributes.Add("rel", "stylesheet")
            myHtmlLink.Attributes.Add("type", "text/css")
            myHtmlLink.Href = "/_layouts/SolidQ.MSS.Controls/Captcha/SolidQCaptchaStyles.css?" & r.Next
            Me.Page.Header.Controls.Add(myHtmlLink)

            Dim s = My.Resources.SolidQCaptchaResource
            Dim stdef As New StringBuilder(s)
            With stdef
                .Replace("@InputHiddenID@", InputID)
                .Replace("@Language@", Language.Trim.ToLower)
            End With
            Me.Controls.Add(New LiteralControl(stdef.ToString))

        Catch ex As Exception
            'Logger.Log(ex.Message & ", StackTrace: " & ex.StackTrace.ToString)
        End Try

    End Sub

    Protected Overloads Overrides Sub Render(ByVal writer As HtmlTextWriter)
        Dim st As New StringWriter
        Dim htmlw As New HtmlTextWriter(st)
        MyBase.Render(htmlw)
        writer.Write(st.GetStringBuilder.ToString)
    End Sub

    Friend Sub GenerateHTML(ByVal writer As HtmlTextWriter)
        Me.EnsureChildControls()
        Render(writer)
    End Sub

End Class
